/**
Bus Planner
Copyright 2011 Ernesto Angel Celis de la Fuente 
<celisdelafuente@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

NOTE: This file depends on js/bpmap.js
**/

var currentLocation;
var detectedLocation;
var browserSupportFlag = new Boolean();
var map;
var stopMarker;
var ne = new google.maps.LatLng(23.835248100318047, -99.06627063964845);
var sw = new google.maps.LatLng(23.649831710015892, -99.21595936035158);
var cityMapBounds = new google.maps.LatLngBounds(sw,ne,true);
var defaultLocation = cityMapBounds.getCenter();
var ftid = $('#ftid').val();
var pano = null;
var infowin = null;
var infoHtml = null;

function initialize() {
	/** TODO The center of the map must change to
	 current Geolocation position of the user or
	 city center if we can't determine real position
	*/
	var mapOptions = {
		zoom: 12,
		center: defaultLocation,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(
		document.getElementById("map_canvas"), 
		mapOptions
	);

	// Get user's current position
	geoLocation();

	// TODO I need to change this to be dynamic too
	// Add Fusion tables layer
	var ft_layer = new google.maps.FusionTablesLayer({
		query: { select: 'ROWID,description,name,color,votes,geometry',
			from: ftid
		}	
	});
	ft_layer.setMap(map);

	// A bunch of event listeners for almost everything
	// the user can do.	
	
	/**
	 * Add an stop marker
	 * */
	$("#addMarker").click(function() {
		if(!stopMarker) {
			if(!currentLocation) {
			// Meebe we didn't accepted to share 
			// our location thus currentLocation
			// will be undefined.
				currentLocation = defaultLocation;
			}
			addStop(currentLocation); 
		}
	});

	/**
	 * Listen for click event on funsiontable layer
	 * */
	google.maps.event.addListener(ft_layer,
		'click',
		function(e) {
			// update infoWindow
			var colorRuta;
			var votosParada;
			switch(e.row["color"].value) {
				case 'small_yellow':
					colorRuta = 'Amarillo';
					break;
				case 'small_blue':
					colorRuta = 'Azúl';
					break;
				case 'small_purple':
					colorRuta = 'Morado';
					break;
				case 'small_red':
					colorRuta = 'Rojo';
					break;
				case 'small_green':
					colorRuta = 'Verde';
					break;
			}
			
			infoHtml = '<div class="googft-info-window" ' +
				'style="font-family: sans-serif"><b>Ruta:' +
				'</b> ' + e.row["name"].value + '<br>' +
				'<b>Descripción:</b> ' +
				e.row["description"].value + 
				'<br><b>Votos:</b> ' +
				e.row["votes"].value + 
				' <a href="#"><img src="/images/thumb-up.png" ' +
				'alt="Voto a favor" onclick="voteUp()" /></a>' +
				'&nbsp;&nbsp;<a href="#"><img ' +
				'src="/images/thumb.png" alt="Voto en contra" ' +
				'onclick="voteDown('+e.latLng+')" /></a>' +
				'<!-- div id="ss-view"></div --></div>';
				e.infoWindowHtml = infoHtml;
		}
	);

}



/**
  If we can't use the browser's geolocation
  capabilities, then we'll center the map to
  the default latitude and longitude.
  */
function handleNoGeoLocation(errorFlag) {
	if(errorFlag == true) {
		// TODO GeoLocation falló, haz algo
	} else {
		// No soporte de Geolocation en browser
	}
	currentLocation = defaultLocation;
	map.setCenter(currentLocation);
}


// Initialize element and widgets, and everything
$('#tv_ftform').ready(function() {
	$('#tv_ftform').hide();
}); 
$(document).ready(function() {
	initialize();
	$("#tv_hide_infobox").click(ftable_edit);
});
